package io.persimmon.core.util;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SecurityUtil {
    public static byte[] sha256ToByte(Object data) {
        MessageDigest digest = null;
        try {
            digest = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        return digest.digest(ObjectUtil.toString(data).getBytes(StandardCharsets.UTF_8));
    }

    public static String sha256ToString(Object data) {
        StringBuilder sb = new StringBuilder();
        byte[] bytes = sha256ToByte(data);
        for (byte b : bytes) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }
}
